package com.sanuo.pico.utils;


import static com.menu.app.util.port.SerialHandle.byteToHex;

import android.app.Activity;
import android.content.Intent;
import android.os.Looper;
import android.util.Log;

import com.menu.app.MyApp;
import com.menu.app.model.Models;
import com.menu.app.util.Utils;
import com.menu.app.util.port.SerialHandler;
import com.menu.app.util.port.SerialManage;
import com.plata.base.utils.WeakHandler;
import com.sanuo.pico.App;
import com.sanuo.pico.act.work.CountAct;
import com.sanuo.pico.act.work.FlowAct;
import com.sanuo.pico.act.work.InfoActivity;
import com.sanuo.pico.act.work.LimitAct;
import com.sanuo.pico.act.work.MsnActivity;
import com.sanuo.pico.act.work.PartAct;
import com.sanuo.pico.act.work.PassActivity;
import com.sanuo.pico.act.work.RedAct;
import com.sanuo.pico.act.work.RentAct;
import com.sanuo.pico.act.work.SettingsActivity;
import com.sanuo.pico.act.work.TempAct;
import com.sanuo.pico.act.work.VrefAct;
import com.sanuo.pico.act.work.WarnAct;
import com.sanuo.pico.act.work.WorkAct;
import com.sanuo.pico.event.Event;
import com.sanuo.pico.event.VerEvent;
import com.sanuo.pico.it.SettingPage;

public class MySerialHandler implements SerialHandler {
    WeakHandler h = new WeakHandler(Looper.getMainLooper());
    public static boolean getData = false;

    @Override
    public int dealMsg(Byte[] data, boolean ignore) {
        try {
            if (data[3] == (byte) 0x83) {
                if (data[4] == (byte) 0x5a && data[5] == (byte) 0xa5) {
                    Models.Companion.get().getStartEvent().setValue(false);
                    if (!Models.Companion.get().getPointLoadingEvent().getValue()) {
                        Models.Companion.get().getPointLoadingEvent().postValue(true);
                    }
                    SerialManage.getInstance().send("5a a5 06 83 5a a5 01 5a a5");
                }
            } else if (data[3] == (byte) 0x82) {
                if (data[4] == 0 && data[5] == (byte) 0x84) {
                    if (!Models.Companion.get().getStartEvent().getValue()) {
//                        Models.Companion.get().getPointLoadingEvent().postValue(false);
                        Models.Companion.get().getStartEvent().postValue(true);
                    }
                    int i = byte2int(data[9]);
                    h.post(new Runnable() {
                        @Override
                        public void run() {
                            gotoPage(i, ignore);
                        }
                    });
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x10) {
                    Event.light.postValue(byte2int(data[7]));
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x13) {
                    Const.p1[0] = byte2int(data[7]);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x14) {
                    Const.p1[1] = byte2int(data[7]);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x15) {
                    Const.p1[2] = byte2int(data[7]);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x16) {
                    Const.p1[3] = byte2int(data[7]);
                    int energy = Const.p1[0] * 1000 + Const.p1[1] * 100 + Const.p1[2] * 10 + Const.p1[3];
                    Event.energy.postValue(energy);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x18) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.p2.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x46) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.p3.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x48) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.p4.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x49) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.p5.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x11) {
                    Event.loading.postValue(byte2int(data[7]));
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x20) {
                    Event.condition.postValue(byte2int(data[7]));
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x21) {
                    Event.ready.postValue(byte2int(data[7]));
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x32) {
                    Event.temp.postValue(byte2int(data[7]));
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x33) {
                    if (!Models.Companion.get().getStartEvent().getValue()) {
//                        Models.Companion.get().getPointLoadingEvent().postValue(false);
                        Models.Companion.get().getStartEvent().postValue(true);
                    }
                    int i = byte2int(data[7]);
                    Event.temp1.postValue(i);
                    Models.Companion.get().getTempEvent().postValue(i);
                } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0xff) {
                    Event.temp2.postValue(byte2int(data[7]));
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x34) {
                    Event.flow.postValue(byte2int(data[7]));
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x35) {
                    if (!Models.Companion.get().getStartEvent().getValue()) {
//                        Models.Companion.get().getPointLoadingEvent().postValue(false);
                        Models.Companion.get().getStartEvent().postValue(true);
                    }
                    int i = byte2int(data[7]);
                    Event.flow1.postValue(i);
                    Models.Companion.get().getFlowEvent().postValue(i);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x28) {
                    int i = byte2int(data[7]);
                    Event.level.postValue(i);
                    h.post(() -> {
                        if (Const.isRent || App.getApp().getTopActivity() instanceof RentAct || isRentPage(Const.currentPage)) {
                            Models.Companion.get().getRentEvent().postValue(i);
                        }
                    });
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x1a) {
                    Event.vb.postValue(byte2int(data[7]));
                    if (Event.vb.is532()) {
                        Event.energy.setMaxMin(250, 40, 10);
                    } else {
                        Event.energy.setMaxMin(500, 80, 20);
                    }
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x72) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vv1.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x60) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMin1.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x61) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMin2.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x62) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMin3.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x63) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMin4.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x64) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMin5.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x65) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMin6.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x66) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMin7.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x67) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMin8.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x68) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMin9.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x69) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMax1.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x6a) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMax2.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x6b) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMax3.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x6c) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMax4.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x6d) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMax5.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x6e) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMax6.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x6f) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMax7.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x70) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMax8.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x71) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vMax9.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x73) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.vv2.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x22) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]) + byteToHex(data[8]) + byteToHex(data[9]);
                    long l = Long.parseLong(tmp, 16);
                    Event.count.postValue(l);
                    h.post(() -> {
                        if (Const.isRent || App.getApp().getTopActivity() instanceof RentAct || isRentPage(Const.currentPage)) {
                            if (Const.is1315()) {
                                Const.rentLimit = l;
                                Models.Companion.get().getPointEvent().postValue(Math.max(l - Const.rented, 0));
                            } else {
                                Models.Companion.get().getPointEvent().postValue(Math.max(l, 0));
                            }
                        }
                    });
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x4c) {
                    Event.redLevel.postValue(byte2int(data[7]));
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x45) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.voltage.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] >= (byte) 0x50 && data[5] <= (byte) 0x54) {
                    Event.warn.postValue(byte2int(data[5]) - 0x50, byte2int(data[7]));
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x29) {
                    Event.limit.postValue(byte2int(data[7]));
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x1b) {
                    String tmp = byteToHex(data[6]) + byteToHex(data[7]);
                    int l = Integer.parseInt(tmp, 16);
                    Event.energyDensity.postValue(l);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x1f) {
                    Event.length.postValue(byte2int(data[7]));
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x90) {
                    Event.show.postValue(byte2int(data[7]));
                } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0x0) {
                    Const.ver[0] = byte2int(data[7]);
                } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0x2) {
                    Const.ver[1] = byte2int(data[7]);
                } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0x4) {
                    Const.ver[2] = byte2int(data[7]);
                } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0x6) {
                    Const.ver[3] = byte2int(data[7]);
                    Utils.setCoreVersion("" + Const.ver[0] + Const.ver[1] + Const.ver[2] + Const.ver[3]);
                    new VerEvent().postValue(0);
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x55) {
                    Event.tip.postValue(byte2int(data[7]));
                } else if (data[4] == (byte) 0x10 && data[5] == (byte) 0x2f) {
                    Event.connect.postValue(byte2int(data[7]));
                }
            } else if (data[3] == (byte) 0xb0) {
                Event.handle.postValue(byte2int(data[4]));
            } else if (data[3] == (byte) 0xb4 && Const.is1315()) {
                String tmp = byteToHex(data[5]) + byteToHex(data[6]) + byteToHex(data[7]) + byteToHex(data[8]);
                Const.rented = Long.parseLong(tmp, 16);
                if (Const.rentLimit > 0) {
                    Models.Companion.get().getPointEvent().postValue(Math.max(Const.rentLimit - Const.rented, 0));
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            getData = true;
        }
        return 0;
    }

    public static int byte2int(byte data) {
        if (data < 0) {
            int data_out = (data & Byte.MAX_VALUE) + 128;
            return data_out;
        }
        return data;
    }

    private void gotoPage(int index, boolean ignore) {
        Log.e("taggg", "goto page " + index + " " + ignore);
        Const.currentPage = index;
        Class c = null;
        Intent i = new Intent();
        if (index <= 2) {
            ;
        } else if (index <= 6) {
            Activity activity = App.getApp().getTopActivity();
            if (activity instanceof WorkAct || activity instanceof SettingPage) {
                activity.finish();
            }
            return;
//            c = ModeActivity.class;
        } else if (index <= 26) {//no page
            return;
        } else if (index <= 30) {
            c = WorkAct.class;
        } else if (index <= 34) {//language
            return;
        } else if (index <= 38) {
            if (App.getApp().getTopActivity() instanceof InfoActivity) {
                i.putExtra("from", 1);
            }
            c = PassActivity.class;
        } else if (index <= 42) {
            c = SettingsActivity.class;
        } else if (index <= 46) {
            c = PartAct.class;
        } else if (index <= 50) {
            c = TempAct.class;
        } else if (index <= 54) {
            c = FlowAct.class;
        } else if (index <= 58) {
            c = VrefAct.class;
        } else if (index <= 62) {
            c = MsnActivity.class;
        } else if (index <= 66) {//no page
            return;
        } else if (index <= 70) {
            c = CountAct.class;
        } else if (index <= 74) {
            c = RedAct.class;
        } else if (index <= 78) {//no page
            return;
        } else if (index <= 82) {
            Activity activity = App.getApp().getTopActivity();
            if (activity instanceof WorkAct) {
                activity.finish();
            } else if (activity instanceof SettingPage) {
                i.putExtra("setting", true);
            }
            c = WarnAct.class;
        } else if (index <= 86) {
            c = RentAct.class;
        } else if (index <= 90) {
            c = LimitAct.class;
        }
        if (c == null || ignore) {
            return;
        }
        i.setClass(MyApp.getApp(), c);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        MyApp.getApp().startActivity(i);
    }

    private boolean isRentPage(int i) {
        return i > 82 && i <= 86;
    }
}
